package com.woniuxy.warehouse.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.sale.PackingList;
import com.woniuxy.commons.entity.sale.Product;
import com.woniuxy.commons.entity.warehouse.dto.StockInDetailsDto;
import com.woniuxy.commons.entity.warehouse.vo.warehouse.TypeAndCodeVo;
import com.woniuxy.commons.result.Result;
import com.woniuxy.warehouse.mapper.StockInDetailsDao;
import com.woniuxy.warehouse.service.StockInDetailsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:SHEAndme
 * @Todo:TODO
 * @DateTime:2022/4/22 10:13
 * @Component:霍格沃兹魔法学院
 **/
@Service
public class StockInDetailsServiceImpl implements StockInDetailsService {
    @Resource
    private StockInDetailsDao stockInDetailsDao;

    @Resource
    RedisTemplate<String, List<PackingList>> redisTemplate;

    @Override
    public Result findAllStockInDetails(Integer currentPage, Integer pageSize, StockInDetailsDto stockInDetailsDto) {
        PageHelper.startPage(currentPage,pageSize);
        List<StockInDetailsDto> stockInDetailsList = stockInDetailsDao.findAll(stockInDetailsDto);
        PageInfo<StockInDetailsDto> of = PageInfo.of(stockInDetailsList);
        Result<PageInfo> listResult = new Result<>();
        if(stockInDetailsList.size()>0){

            listResult.setData(of);
            listResult.setCode(200);
            listResult.setMessage("查询成功");
        }else {
            listResult.setData(null);
            listResult.setCode(500);
            listResult.setMessage("数据为空");
        }
        return listResult;
    }

    @Override
    public Result findAllInventory(Integer currentPage, Integer pageSize, TypeAndCodeVo typeAndCodeVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Product> productsList = stockInDetailsDao.findAllInventory(typeAndCodeVo);
        PageInfo<Product> of = PageInfo.of(productsList);
        Result<PageInfo> listResult = new Result<>();
        if (productsList.size()>0){
            listResult.setData(of);
            listResult.setCode(200);
            listResult.setMessage("查询成功");
        }else {
            listResult.setData(null);
            listResult.setCode(500);
            listResult.setMessage("数据为空");
        }
        return listResult;
    }

    /**
     * Description : TODO  用来判断key是否设置过并且还未过期
     * TODO 从redis中获取key对应的过期时间;
     *  * 如果该值有过期时间，就返回相应的过期时间;
     *  * 如果该值没有设置过期时间，就返回-1;
     *  * 如果没有该值，就返回-2;
     * ChangeLog : 1. 创建 (2022/4/25 13:12 [CHL]);
     ** @param key
     * *  @return boolean
     **/
    @Override
    public Long isExpire(String key){
        return redisTemplate.opsForValue().getOperations().getExpire(key);
    }
}
